﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;

namespace FangChanCrm.Controllers
{
    public class FangYuanXinXiController : Controller
    {
        private readonly It_FangYuanXinXiBiaoService t_FangYuanXinXiBiaoService;
        private readonly It_FangYuanBiaoService t_FangYuanBiaoService;

        public FangYuanXinXiController(It_FangYuanXinXiBiaoService fangYuanXinXiBiaoService, It_FangYuanBiaoService fangYuanBiaoService, IConfiguration configuration)
        {
            t_FangYuanXinXiBiaoService = fangYuanXinXiBiaoService;
            t_FangYuanBiaoService = fangYuanBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IActionResult FangYuanXinXiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult FangYuanCengUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult FangYuanXinXiLouHaoUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult FangYuanXinXiAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult FangYuanXinXiUpdate()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public string GetLouHao()
        {
            string xiaoqu = Request.Form["xiaoqu"];
            StringBuilder Json = new StringBuilder();
            List<t_FangYuanXinXiBiao> fy = t_FangYuanXinXiBiaoService.GetLouHao(Convert.ToInt32(xiaoqu));
            Json.Append("[");
            for (int i = 0; i < fy.Count; i++)
            {
                t_FangYuanXinXiBiao fangyuan = fy[i];
                Json.Append("{\"id\":\"" + fangyuan.Id + "\",\"louHao\":\"" + fangyuan.LouHao + "\"}");
                if ((i + 1) == fy.Count)
                {

                }
                else
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string GetXiaoQu()
        {
            StringBuilder Json = new StringBuilder();
            List<t_FangYuanBiao> fy = t_FangYuanBiaoService.GetList1();
            Json.Append("[");
            for (int i = 0; i < fy.Count; i++)
            {
                t_FangYuanBiao fangyuan = fy[i];
                Json.Append("{\"id\":\"" + fangyuan.Id + "\",\"xiaoQuMingCheng\":\"" + fangyuan.XiaoQuMingCheng + "\"}");
                if ((i + 1) == fy.Count)
                {

                }
                else
                {
                    Json.Append(",");
                }
            }
            Json.Append("]");
            return Json.ToString();
        }

        public string PiLiangTianJia()
        {
            try
            {
                string pixiaoqu = Request.Form["pixiaoqu"];
                string kaishilouhao = Request.Form["kaishilouhao"];
                string jieshulouhao = Request.Form["jieshulouhao"];
                string kaishicengshu = Request.Form["kaishicengshu"];
                string jieshuchengshu = Request.Form["jieshuchengshu"];
                string kaishimenshu = Request.Form["kaishimenshu"];
                string jieshumenshu = Request.Form["jieshumenshu"];

                int louhaoxhuanshu = (Convert.ToInt32(jieshulouhao) - Convert.ToInt32(kaishilouhao)) + 1;
                int cengxhuanshu = (Convert.ToInt32(jieshuchengshu) - Convert.ToInt32(kaishicengshu)) + 1;
                int menxhuanshu = (Convert.ToInt32(jieshumenshu) - Convert.ToInt32(kaishimenshu)) + 1;

                for (int i = 0; i < louhaoxhuanshu; i++)
                {
                    for (int j = 0; j < cengxhuanshu; j++)
                    {
                        for (int c = 0; c < menxhuanshu; c++)
                        {
                            t_FangYuanXinXiBiao fangyuanxinxi = new t_FangYuanXinXiBiao();
                            fangyuanxinxi.XiaoQuId = Convert.ToInt32(pixiaoqu);
                            fangyuanxinxi.LouHao = (Convert.ToInt32(kaishilouhao) + i).ToString();
                            fangyuanxinxi.Ceng = Convert.ToInt32(kaishicengshu) + j;
                            fangyuanxinxi.Men = (Convert.ToInt32(kaishimenshu) + c).ToString();
                            fangyuanxinxi.ZhuangTai = "未出售";
                            if (t_FangYuanXinXiBiaoService.GetShuJu(fangyuanxinxi))
                            {
                                t_FangYuanXinXiBiaoService.Add(fangyuanxinxi);
                            }
                            else
                            {
                            }
                        }
                    }
                }
                return "1";
            }
            catch (Exception)
            {
                return "2";
            }
        }

        public string DanGeTianJia()
        {
            try
            {
                string danxiaoqu = Request.Form["danxiaoqu"];
                string louhao = Request.Form["louhao"];
                string cheng = Request.Form["cheng"];
                string men = Request.Form["men"];
                t_FangYuanXinXiBiao fangyuanxinxi = new t_FangYuanXinXiBiao();
                fangyuanxinxi.XiaoQuId = Convert.ToInt32(danxiaoqu);
                fangyuanxinxi.LouHao = louhao.ToString();
                fangyuanxinxi.Ceng = Convert.ToInt32(cheng);
                fangyuanxinxi.Men = men;
                fangyuanxinxi.BeiZhu1 = men;
                fangyuanxinxi.ZhuangTai = "未出售";
                if (t_FangYuanXinXiBiaoService.GetShuJu(fangyuanxinxi))
                {
                    t_FangYuanXinXiBiaoService.Add(fangyuanxinxi);
                    return "1";
                }
                else
                {
                    return "3";
                }
            }
            catch (Exception)
            {

                return "2";
            }
        }

        public string GetShuJuXinXi()
        {
            StringBuilder Json = new StringBuilder();
            string id = Request.Form["id"];
            t_FangYuanXinXiBiao fhangwu = t_FangYuanXinXiBiaoService.GetId(Convert.ToInt32(id));
            t_FangYuanBiao fw = t_FangYuanBiaoService.GetIdAsync(Convert.ToInt32(fhangwu.XiaoQuId));
            Json.Append("[");
            Json.Append("{\"danxiaoqu\":\"" + fw.XiaoQuMingCheng + "\",\"louhao\":\"" + fhangwu.LouHao + "\",\"cheng\":\"" + fhangwu.Ceng + "\",\"men\":\"" + fhangwu.Men + "\",\"zhuangtai\":\"" + fhangwu.ZhuangTai + "\",\"xiaoquidzhi\":\"" + fhangwu.XiaoQuId + "\"}");
            Json.Append("]");
            return Json.ToString();
        }

        public string XuGai()
        {
            bool panduan = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var FangYuanXinXi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                FangYuanXinXi.ZhuangTai = Request.Form["zhuangtai"].ToString();
                panduan = _context.SaveChanges() > 0;
            }
            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string ShanChu()
        {
            string id = Request.Form["id"];

            bool panduan = t_FangYuanXinXiBiaoService.DelFangFa(Convert.ToInt32(id));

            if (panduan)
            {
                return "1";
            }
            else
            {
                return "0";
            }
        }

        public string ChaKanFangWuShu()
        {

            StringBuilder Json = new StringBuilder();

            string xiaoqu = Request.Form["xiaoqu"];
            string louhao = Request.Form["louhao"];

            t_FangYuanBiao fw = t_FangYuanBiaoService.GetIdAsync(Convert.ToInt32(xiaoqu));
            List<t_FangYuanXinXiBiao> fhangwu = t_FangYuanXinXiBiaoService.GetFangWuShuJu(Convert.ToInt32(xiaoqu), louhao);

            string xiaoqumingcheng = fw.XiaoQuMingCheng + "  " + louhao + "号楼";
            int zoushu = fhangwu.Count;
            int chushou = 0;
            int weichushou = 0;
            int shenhezhong = 0;

            for (int i = 0; i < fhangwu.Count; i++)
            {
                t_FangYuanXinXiBiao fy = fhangwu[i];
                if (fy.ZhuangTai == "已出售")
                {
                    chushou = chushou + 1;
                }
                else if (fy.ZhuangTai == "未出售")
                {
                    weichushou = weichushou + 1;
                }
                else if (fy.ZhuangTai == "审核中")
                {
                    shenhezhong = shenhezhong + 1;
                }
                else
                {

                }
            }

            Json.Append("[");
            Json.Append("{\"zoushu\":\"" + zoushu + "\",\"chushou\":\"" + chushou + "\",\"weichushou\":\"" + weichushou + "\",\"shenhezhong\":\"" + shenhezhong + "\",\"xiaoqumingcheng\":\"" + xiaoqumingcheng + "\"}");
            Json.Append("]");

            return Json.ToString();
        }

        public string XuGaiLouHao()
        {
            try
            {
                string xiaoqu = Request.Form["xiaoqu"];
                string louhao = Request.Form["louhao"];
                string xuegailouhaoshuju = Request.Form["xuegailouhaoshuju"];
                List<t_FangYuanXinXiBiao> fyxx = t_FangYuanXinXiBiaoService.GetFangWuShuJu(Convert.ToInt32(xiaoqu), louhao);
                for (int j = 0; j < fyxx.Count; j++)
                {
                    t_FangYuanXinXiBiao fangyuanxinxi = fyxx[j];
                    fangyuanxinxi.LouHao = xuegailouhaoshuju;
                    if (t_FangYuanXinXiBiaoService.GetShuJu(fangyuanxinxi))
                    {
                        t_FangYuanXinXiBiaoService.DelFangFa(fangyuanxinxi.Id);
                        fangyuanxinxi.Id = 0;
                        t_FangYuanXinXiBiaoService.Add(fangyuanxinxi);
                    }
                    else
                    {
                        t_FangYuanXinXiBiaoService.DelFangFa(fangyuanxinxi.Id);
                    }
                }
                return "1";
            }
            catch (Exception)
            {
                return "2";
            }
        }

        public string XuGaiCeng()
        {
            try
            {
                string xiaoqu = Request.Form["xiaoqu"];
                string louhao = Request.Form["louhao"];
                string cengshu = Request.Form["cengshu"];
                string genggaicengshu = Request.Form["genggaicengshu"];
                List<t_FangYuanXinXiBiao> fyxx = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(xiaoqu), louhao, Convert.ToInt32(cengshu));
                for (int j = 0; j < fyxx.Count; j++)
                {
                    t_FangYuanXinXiBiao fangyuanxinxi = fyxx[j];
                    fangyuanxinxi.Ceng = Convert.ToInt32(genggaicengshu);
                    if (t_FangYuanXinXiBiaoService.GetShuJu(fangyuanxinxi))
                    {
                        t_FangYuanXinXiBiaoService.DelFangFa(fangyuanxinxi.Id);
                        fangyuanxinxi.Id = 0;
                        t_FangYuanXinXiBiaoService.Add(fangyuanxinxi);
                    }
                    else
                    {
                        t_FangYuanXinXiBiaoService.DelFangFa(fangyuanxinxi.Id);
                    }
                }
                return "1";
            }
            catch (Exception)
            {
                return "2";
            }
        }

        public string ChaKanXianShi()
        {
            StringBuilder Json = new StringBuilder();
            string yanse = "";
            string xiaoqu = Request.Form["xiaoqu"];
            string louhao = Request.Form["louhao"];

            List<t_FangYuanXinXiBiao> cengshu = t_FangYuanXinXiBiaoService.GetCeng(Convert.ToInt32(xiaoqu), louhao);

            for (int i = 0; i < cengshu.Count; i++)
            {
                int kuandu = 0;
                string htmlMen = "";
                t_FangYuanXinXiBiao fyxinxiceng = cengshu[i];
                List<t_FangYuanXinXiBiao> fyxx = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(xiaoqu), louhao, fyxinxiceng.Ceng);
                for (int j = 0; j < fyxx.Count; j++)
                {
                    t_FangYuanXinXiBiao fy = fyxx[j];
                    if (fy.ZhuangTai == "已出售")
                    {
                        yanse = "red";
                    }
                    else if (fy.ZhuangTai == "未出售")
                    {
                        yanse = "gray";
                    }
                    else if (fy.ZhuangTai == "审核中")
                    {
                        yanse = "green";
                    }
                    else
                    {
                        yanse = "orange";
                    }
                    string men = fy.Men;
                    kuandu = fyxx.Count * 152;
                    kuandu = kuandu + 20;
                    string shi = "";
                    if (fy.BeiZhu1 != "" && fy.BeiZhu1 != null)
                    {
                        shi = fy.BeiZhu1;
                    }
                    else
                    {
                        string menshuzhi = "";
                        if (Convert.ToInt32(fy.Men) > 9)
                        {
                            menshuzhi = Convert.ToInt32(fy.Men).ToString();
                        }
                        else
                        {
                            menshuzhi = "0" + Convert.ToInt32(fy.Men).ToString();
                        }
                        shi = fy.Ceng + "" + menshuzhi + "室";
                    }
                    htmlMen +=
                         "<a onclick=\"Javascript:window.open('../FangYuanXinXi/FangYuanXinXiUpdate?fangyuan=" + fy.Id + "')\" target=\"_blank\" \">" +
                         "<div id=\"exampleContext1\" data-toggle=\"" + fy.Id + "\"  style=\"width: 142px; height: 75px; background-color: white; border:4px solid " + yanse + "; margin-top: 5px; border-radius: 10px;  border-radius: 10px; margin-left: 10px; margin-bottom: 10px; float: left; color: black;\">" +
                         "<div style=\"margin-top: 15%; text-align: center;\">" + shi + "</div>" +
                         "</div>" +
                         "</a>";
                }
                string result =
                "<span style=\"width: " + kuandu + "px; height: 150px; display: inline-block; background-color: rgb(241,245,246); margin-top: 10px; margin-left: 5px; margin-bottom: 5px;\">" +
                 "<span onclick='FangYuanCengXueGai(" + cengshu[i].Ceng + ")' style=\"display: inline-block; width: 100%; height: 25px; text-align: center; margin: 8px 20px 10px -10px;\">" + cengshu[i].Ceng + "层 <a href='#' name='" + cengshu[i].Ceng + "'></a></span><br />" +
                               htmlMen +
                               "</span><br />";
                Json.Append(result);
            }
            return Json.ToString();
        }

    }
}
